from functools import wraps

'''
def decorator_name(f):
    @wraps(f)
    def decorated(*args,**kw):
        if not can_run:
            return "Function not can run"
        return f(*args,**kw)
    return decorated

@decorator_name
def fun():
    print("function is running")

can_run = True
fun()

can_run = False
fun()


def logit(logfile='out.log'):
    def log_decotator(func):
        @wraps(func)
        def wrapped_function(*args, **kw):
            log_string = func.__name__ + "was called"
            print(log_string)
            with open(logfile, 'a') as f:
                f.write(log_string + '\n')
            return func(*args, **kw)

        return wrapped_function

    return log_decotator


@logit()
def myfunc1():
    pass


myfunc1()

@logit('out2.log')
def myfunc2():
    pass

myfunc2()
'''


class logit(object):
    def __init__(self, logfile='out.log'):
        self.logfile = logfile

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kw):
            log_string = func.__name__ + "new\n"
            print(log_string)
            with open(self.logfile, 'a') as f:
                f.write(log_string)
            self.notify()
            return func(*args, **kw)

        return wrapped_function

    def notify(self):
        pass


@logit()
def myfunc1():
    pass


myfunc1()


class email_log(logit):
    def __init__(self, email='tlkt99@126.com', *args, **kw):
        self.email = email
        super(logit, self).__init__(*args, **kw)

    def notify(self):
        pass


@email_log()
def myfunc2():
    pass


myfunc2()
